package com.pengtu.gsj.service.impl.check.lzgl.enforSup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.enforSup.LawEnforEvaluaService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class LawEnforEvaluaServiceImpl implements LawEnforEvaluaService {
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = Arrays.asList("0", "0", "0", "0");
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取考核制度/方法
		List<Attachment> khzdList = systemManageService.getAllCheckAttachments("zfpykh_khzd");
		//获取年度通知通报文件
		List<Attachment> tztbList = systemManageService.getCheckAttachmentByTag(startTime, endTime, "zfpykh_tztb");
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull + yearPart*systemManageService.getCheckYear(endTime, startTime, 1);	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		if (khzdList.size() > 0) {
			total += yearFull;
		} 
		for (int i = 0; i < years.length; i++) {
			checkResult = Arrays.asList("0", "0", "0");
			checkResult.set(0, String.valueOf(years[i]));
			for (Attachment attachment : tztbList) {
				if ( years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", attachment.getFileTime()))){
					checkResult.set(1, "1");
				}
			}
			
			if ("1".equals(checkResult.get(1))) {
				total += yearPart;
				checkResult.set(2, String.valueOf(yearPart));
			}
			resultLists.add(checkResult);
		}
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("resultLists", resultLists);
		map.put("yearFull", yearFull);
		map.put("khzdList", khzdList);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
